"use strict";
const electron = require("electron");
const { ipcMain } = require("electron");
const BrowserWindow = electron.BrowserWindow;
const app = electron.app;
const { autoUpdater } = require("electron-updater");
const uploadUrl = "https://zhengjingjiaoyu.oss-cn-guangzhou.aliyuncs.com/update/";
autoUpdater.setFeedURL(uploadUrl);
const path = require("path");
const getMac = require("getmac").default;
const createWindow = () => {
  const win = new BrowserWindow({
    width: 1200,
    height: 800,
    minWidth: 960,
    minHeight: 600,
    webPreferences: {
      nodeIntegration: true,
      // 设置是否在页面中启用 Node.js 集成模式
      contextIsolation: true,
      // 设置是否启用上下文隔离模式。
      webviewTag: true,
      // 开启webview
      // frame: false // 这里设置为false，禁用默认的浏览器窗口框架。
      preload: path.resolve(__dirname, "preload.js"),
      partition: String(+/* @__PURE__ */ new Date())
    }
  });
  win.setTitle("正晶教育");
  win.loadFile(path.join(__dirname, "../dist/index.html"));
  let mac = getMac();
  win.webContents.send("mac", mac);
  autoUpdater.autoDownload = true;
  autoUpdater.checkForUpdates();
  autoUpdater.on("error", function(error) {
    console.log("更新错误", error);
    win.webContents.send("uploadError", error);
  });
  autoUpdater.on("checking-for-update", function() {
    console.log("开始检测更新");
  });
  autoUpdater.on("update-available", function(info) {
    console.log("当发现有可用更新的时候触发", info);
  });
  autoUpdater.on("update-not-available", function(info) {
    console.log("当发现版本为最新版本触发", info);
  });
  autoUpdater.on("download-progress", function(progressObj) {
    console.log(progressObj.percent);
    win.webContents.send("downloadProgress", progressObj.percent);
  });
  autoUpdater.on("update-downloaded", function(event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) {
    console.log("下载完成，给渲染进程发送消息");
    win.webContents.send("isUpdateNow");
    ipcMain.on("updateNow", (e, arg) => {
      console.log("开始更新");
      autoUpdater.quitAndInstall();
    });
  });
};
app.whenReady().then(createWindow);
